package com.whmnx.chitchat.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.cy.cyflowlayoutlibrary.FlowLayoutAdapter;
import com.cy.cyflowlayoutlibrary.FlowLayoutScrollView;
import com.tencent.qcloud.tim.uikit.utils.DateTimeUtil;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.bean.BaseBean;
import com.whmnx.chitchat.bean.DynaminBean;
import com.whmnx.chitchat.bean.LoginBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.main.LoginActivity;
import com.whmnx.chitchat.main.RegisterActivity;
import com.whmnx.chitchat.utils.ImageUtil;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;
import com.whmnx.chitchat.view.CommonDialog;
import com.whmnx.chitchat.view.FlowLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author : huang
 * date   : 2019/10/21 0021 15:06
 * version: 1.0
 * @author Administrator
 */
public class DynamicAdapter extends CommonRecyclerAdapter<DynaminBean.ForumListBean> {
    private Context context;
    private List<Boolean> alreadyLikeList = new ArrayList<>();
    private List<String> lists = new ArrayList<>();
    private ImageView view;

    private ReplyListener mReplyListener;

    public void setReplyListener(ReplyListener replyListener) {
        mReplyListener = replyListener;
    }


    public interface ReplyListener {
      //  void commentReplyTopUser(DynamicCommentBean item, int position);

        void commentReplyUser(String item, String  forumComentId,int position);

     //   void dianz(DynamicCommentBean item, int position);
    }


    public DynamicAdapter(Context context) {
        super(context, R.layout.item_list_dynamic);
        this.context = context;
    }


    @Override
    public void onUpdate(BaseAdapterHelper helper, DynaminBean.ForumListBean item, int position) {
        ImageUtil.setImage(helper.getView(R.id.civ_personal_head_image), (String) item.getForum().getUserInfo_HeadImg());
        helper.setText(R.id.tv_name, (String) item.getForum().getUserInfo_NickName());
        long t = UItils.dateToStamp(item.getForum().getForum_CreateTime());
        helper.setText(R.id.tv_time, DateTimeUtil.getTimeFormatText(new Date(t*1000)));
      //  helper.setText(R.id.tv_time, (String) item.getForum().getForum_CreateTime());
        helper.setText(R.id.tv_dynamic_detail, (String) item.getForum().getForum_Content());
     //   helper.setText(R.id.tv_msg, item.getForum_CommentNum() + "");
      //  helper.setText(R.id.tv_like,  item.getForum_PraiseNum() + "");
         GridView view = helper.getView(R.id.rv_dynamic_photos);
         ImageView imageView = helper.getView(R.id.iv_like);
         TextView tv_like = helper.getView(R.id.tv_like);
         if (item.getForum().getUserInfo_ID().equals(SPUtil.getString(Constants.USERID))){
             helper.setVisible(R.id.iv_del, View.VISIBLE);
         }else {
             helper.setVisible(R.id.iv_del, View.GONE);
         }

         if (item.getLikes().size() != 0) {
             //FlowLayout flowLayout =  helper.getView(R.id.flow);
             helper.setVisible(R.id.ib_like2, View.VISIBLE);
             helper.setVisible(R.id.flow,View.VISIBLE);
         //   RecyclerView rlLike = helper.getView(R.id.rl_like);
        //    rlLike.setItemAnimator(new DefaultItemAnimator());
         //   LikeAdapter likeAdapter = new LikeAdapter(context);
        //    rlLike.setAdapter(likeAdapter);
         //   likeAdapter.addAll((List<DynaminBean.ForumListBean.LikesBean>) item.getLikes());
             lists.clear();
             for (int i = 0; i < item.getLikes().size(); i++) {
                 lists.add(item.getLikes().get(i).UserInfo_NickName);
               /*  for (int i1 = 0; i1 < 10; i1++) {
                     lists.add(i1 + "名字");
                 }*/
             }
             FlowLayoutAdapter  flowLayoutAdapter = new FlowLayoutAdapter<String>(lists) {
                 @Override
                 public void bindDataToView(ViewHolder holder, int position, String bean) {
                     holder.setText(R.id.tv_name,bean);
                 }

                 @Override
                 public void onItemClick(int position, String bean) {

                 }

                 @Override
                 public int getItemLayoutID(int position, String bean) {
                     return R.layout.item_list_like2;
                 }
             };
             ((FlowLayoutScrollView)helper.getView(R.id.flow)).setAdapter(flowLayoutAdapter);
        }else {
            helper.setVisible(R.id.ib_like2, View.GONE);
            helper.setVisible(R.id.flow, View.GONE);
            //线条
            helper.setVisible(R.id.view, View.GONE);
        }

        RecyclerView rlComment = helper.getView(R.id.rl_comment);
        if (item.getComment().size() == 0){
            rlComment.setVisibility(View.GONE);
            helper.setVisible(R.id.view, View.GONE);
        }else {
            rlComment.setVisibility(View.VISIBLE);
            helper.setVisible(R.id.view, View.VISIBLE);
           // RecyclerView rlComment = helper.getView(R.id.rl_comment);
            rlComment.setLayoutManager(new LinearLayoutManager(context));
            rlComment.setItemAnimator(new DefaultItemAnimator());
            CommentAdapter commentAdapter = new CommentAdapter(context);
            rlComment.setAdapter(commentAdapter);
            commentAdapter.addAll((List<DynaminBean.ForumListBean.CommentBean>) item.getComment());

            commentAdapter.setReplyListener(new ReplyListener() {
                @Override
                public void commentReplyUser(String item, String forumComentId, int position) {
                    mReplyListener.commentReplyUser(item,forumComentId ,position);
                }
            });
        }

        if (item.getLikes().size() == 0 && item.getComment().size() ==0 ){
            helper.setVisible(R.id.ll, View.GONE);
        }else {
            helper.setVisible(R.id.ll, View.VISIBLE);
            helper.setVisible(R.id.view, View.VISIBLE);
        }

        helper.getView(R.id.iv_like).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             /*   if (mReplyListener != null) {
                    mReplyListener.commentReplyUser(item, position);
                }*/
            }
        });

        helper.getView(R.id.iv_msg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mReplyListener != null) {
                    mReplyListener.commentReplyUser(item.getForum().getForum_ID(),"" ,position);
                }
            }
        });

        helper.getView(R.id.iv_like).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mReplyListener != null) {
                    mReplyListener.commentReplyUser(item.getForum().getForum_ID(),"dian" ,position);
                }
            }
        });

        helper.getView(R.id.iv_del).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s2 = "确定删除吗?";
                new CommonDialog(context, R.style.dialog, s2, (dialog, confirm) -> {
                    if (confirm) {
                        Map<String, Object> params = new HashMap<>();
                        params.put("UserID",SPUtil.getString(Constants.USERID));
                        params.put("ForumID",item.getForum().getForum_ID());
                        HttpClient.get(context, Constants.GETDELETEFORUM, params,new CallBack<LoginBean>() {
                            @Override
                            public void onSuccess(LoginBean result) {
                                getData().remove(position);
                                notifyItemRemoved(position);
                            }
                        });
                    }
                    dialog.dismiss();
                }).show();
            }
        });

        //9宫格图片
        List<String> list = new ArrayList<>();
        FansImagesAdpter fansImagesAdpter = new FansImagesAdpter(context, list);
        view.setAdapter(fansImagesAdpter);
        if (!TextUtils.isEmpty(item.getForum().getForum_Imgs()) && item.getForum().getForum_Imgs().contains(",")) {
            String [] split = item.getForum().getForum_Imgs().split(",");
            list.addAll(Arrays.asList(split));
            fansImagesAdpter.notifyDataSetChanged();
        }else {
            if (TextUtils.isEmpty(item.getForum().getForum_Imgs())){
                list.clear();
                fansImagesAdpter.notifyDataSetChanged();
            }else {
                list.add(item.getForum().getForum_Imgs());
                fansImagesAdpter.notifyDataSetChanged();
              //  FansImagesAdpter fansImagesAdpter = new FansImagesAdpter(context, list);
              //  view.setAdapter(fansImagesAdpter);
            }
        }
    }
}

